//
//  OpeningViewController.swift
//  MyWeiBlog
//
//  Created by laichunhui on 15/9/28.
//  Copyright © 2015年 lch. All rights reserved.
//

import UIKit

class LoginViewController: UIViewController, PlatformLoginViewDelegate {
    
    private lazy var matte:UIView =  UIView(frame: ScreenBounds)
    private lazy var loginBar:PlatformLoginView =  PlatformLoginView(frame: CGRectMake(0, ScreenHeight + LoginBarH, ScreenWidth, LoginBarH))

    override func viewDidLoad() {
        
    }

    @IBAction func loginByUM() {
        //添加蒙版
        matte.backgroundColor = UIColor.blackColor()
        matte.alpha = 0.3
        view.addSubview(matte)
        
        //添加登录条
        view.addSubview(loginBar)
        loginBar.delegate = self
        
        UIView.animateWithDuration(0.5) { () -> Void in
           self.loginBar.frame = CGRectMake(0, self.matte.height - LoginBarH, self.matte.width, LoginBarH)
        }
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        UIView.animateWithDuration(0.3, animations: { () -> Void in
            self.loginBar.frame = CGRectMake(0, ScreenHeight + LoginBarH, ScreenWidth, 0)
            }) { (_) -> Void in
                self.matte.removeFromSuperview()
        }
    }
//MARK: -PlatformLoginViewDelegate
    func platformLoginViewForWeChatLogin(platformLoginView: PlatformLoginView)
    {
        //func login(正常的参数:类型,(闭包的参数1:类型,闭包的参数2:类型)->(闭包的返回类型)){}
        let snsPlatform: UMSocialSnsPlatform = UMSocialSnsPlatformManager.getSocialPlatformWithName(UMShareToWechatSession)
        //response:UMSocialResponseEntity
        snsPlatform.loginClickHandler(self, UMSocialControllerService.defaultControllerService(), true, {response in
            
            if response.responseCode == UMSResponseCodeSuccess {
                
                let snsAccount:UMSocialAccountEntity = UMSocialAccountManager.socialAccountDictionary()[UMShareToWechatSession] as! UMSocialAccountEntity
                
                print("username is \(snsAccount.userName), uid is \(snsAccount.usid), token is \(snsAccount.accessToken)url is \(snsAccount.iconURL)")
        
//                    print(response1.data)
//                })
            }
            
        })
    }
    func platformLoginViewForSinaLogin(platformLoginView: PlatformLoginView)
    {
        //func login(正常的参数:类型,(闭包的参数1:类型,闭包的参数2:类型)->(闭包的返回类型)){}
        let snsPlatform: UMSocialSnsPlatform = UMSocialSnsPlatformManager.getSocialPlatformWithName(UMShareToSina)
        
        snsPlatform.loginClickHandler(self, UMSocialControllerService.defaultControllerService(), true, {response in
            
            if response.responseCode == UMSResponseCodeSuccess {
                
                let snsAccount:UMSocialAccountEntity = UMSocialAccountManager.socialAccountDictionary()[UMShareToSina] as! UMSocialAccountEntity
//                print("username is \(snsAccount.userName), uid is \(snsAccount.usid), token is \(snsAccount.accessToken) url is \(snsAccount.iconURL)")
                
                // 把自有账号添加到本地账号中
                UMSocialAccountManager.setSnsAccount(snsAccount)
                
                /*
                expires_in=665554
                scheme is sina.5608de9de0f55afaae0018e6://
                username is 脚后跟迎着风,
                uid is 2253117990,
                token is 2.00kuqT9C03t9ZI4c16331e70tA8WOB
                url is http://tp3.sinaimg.cn/2253117990/180/5727964648/1
                */
                //获取accestoken以及新浪用户信息，得到的数据在回调Block对象形参respone的data属性
                //                UMSocialDataService.defaultDataService().requestSnsInformation(UMShareToSina, completion: { (response1: UMSocialResponseEntity) -> Void in
                
                // 创建用户账户模型，as! 将一个对象视为什么类型， !/? 取决于参数的需求
                let userAccount = UserAccount(dict: [String : AnyObject]())
                userAccount.access_token = snsAccount.accessToken
                userAccount.expiresDate = snsAccount.expirationDate
                userAccount.screen_name = snsAccount.userName
                userAccount.uid = snsAccount.usid
                userAccount.avatar_large = snsAccount.iconURL
                //存到沙盒
                userAccount.saveUserAccount()
//                print("---------")
//                print(userAccount.expiresDate)
                
                //跳转到首页
                NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewControllerNotification, object: TabBarControlle())
            }

        })
    }
    
}